﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using cklib;
using Silmoon.Arrays;
using Silmoon.Threading;
using System.Diagnostics;

namespace Cooky.Client
{
    public class SimpleAsyncWatcher
    {
        Socket socket;
        public event objHandler WatchCompleteAsync;
        string id = null;
        public SimpleAsyncWatcher(string id)
        {
            this.id = id;
        }
        public void StartCheck(IPAddress ip, int port, string host)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            bool success = false;
            int statuscode = 0;
            if (ip.AddressFamily == AddressFamily.InterNetwork)
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
                socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

            socket.ReceiveTimeout = 50000;

            socket.BeginConnect(ip, port, new AsyncCallback(delegate(IAsyncResult ar)
            {
                try
                {
                    socket.EndConnect(ar);
                }
                catch { }

                if (socket.Connected)
                {
                    byte[] sendData = Encoding.Default.GetBytes("GET / HTTP/1.1\r\nHOST: " + host + "\r\n\r\n");
                    socket.BeginSend(sendData, 0, sendData.Length, SocketFlags.None, new AsyncCallback(delegate(IAsyncResult ar1)
                    {
                        try
                        {
                            byte[] recvData = new byte[512];
                            int recvLength = socket.Receive(recvData);
                            string recvString = Encoding.Default.GetString(recvData, 0, recvLength);
                            string[] recvArray = recvString.Split(new string[] { " " }, StringSplitOptions.None);
                            if (recvArray[0] == "HTTP/1.1" && recvArray.Length > 1)
                            {
                                statuscode = int.Parse(recvArray[1]);
                                success = true;
                                onWatchCompleteAsync(new object[] { success, statuscode, id, sw });
                            }
                            else
                            {
                                success = false;
                                onWatchCompleteAsync(new object[] { success, statuscode, id, sw });
                            }
                        }
                        catch
                        {
                            success = false;
                            onWatchCompleteAsync(new object[] { success, statuscode, id, sw });
                        }

                    }), null);
                }
                else
                {
                    success = false;
                    onWatchCompleteAsync(new object[] { success, statuscode, id, sw });
                }
            }), null);
        }
        void onWatchCompleteAsync(object obj)
        {
            object[] obj1 = (object[])obj;
            Stopwatch sw = (Stopwatch)obj1[3];
            sw.Stop();
            if (socket != null)
                socket.Close();
            if (WatchCompleteAsync != null) WatchCompleteAsync(obj);
        }

        #region IID 成员

        public string ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        #endregion
    }
}
public delegate void objHandler(object obj);
